package org.anti996.bankms.controller;


import org.anti996.bankms.dao.UserDao;
import org.anti996.bankms.model.Account;
import org.anti996.bankms.util.IOUtil;
import org.anti996.bankms.util.MessageUtil;
import org.anti996.bankms.view.UserInfoUpdateView;

import java.util.List;

public class UserInfoUpdateController extends ControllerBase {
    private final Account account_;

    public UserInfoUpdateController(Account account) {
        super(new UserInfoUpdateView());
        account_ = account;
    }

    @Override
    public void deal() {
        super.deal();
        List<Integer> orders = IOUtil.parseOrders();
        if (orders == null) {
            wrongInputAndRetry();
            return;
        }
        for (int oi : orders) {
            if (oi < 1 || 5 < oi) {
                wrongInputAndRetry();
                return;
            }
        }
        boolean result;
        for (int order : orders) {
            switch (order) {
                case 1 -> {
                    MessageUtil.info("请输入新密码：");
                    String password = IOUtil.parsePassword();
                    while (true) {
                        // TODO 一直输入不对怎么办
                        MessageUtil.info("请重新输入新密码：");
                        if (IOUtil.parsePassword().equals(password)) {
                            result = UserDao.updatePasswd(account_, password);
                            if (result) {
                                MessageUtil.success("密码修改成功");
                                account_.setPasswd(password);
                            } else MessageUtil.error("[内部错误]密码修改失败");
                            break;
                        } else {
                            MessageUtil.warning("输入密码不同");
                        }
                    }
                }
                case 2 -> {
                    MessageUtil.info("请输入新名字：");
                    String name = IOUtil.parseInput();
                    result = UserDao.updateName(account_, name);
                    if (result) {
                        MessageUtil.success("姓名修改成功");
                        account_.setName(name);
                    } else MessageUtil.error("[内部错误]姓名修改失败");
                }
                case 3 -> {
                    MessageUtil.info("请输入新身份证号：");
                    String idnumber = IOUtil.parseInput();
                    result = UserDao.updateIdnumber(account_, idnumber);
                    if (result) {
                        MessageUtil.success("身份证号修改成功");
                        account_.setIdnumber(idnumber);
                    } else MessageUtil.error("[内部错误]身份证号修改失败");
                }
                case 4 -> {
                    MessageUtil.info("请输入新籍贯：");
                    String hometown = IOUtil.parseInput();
                    result = UserDao.updateHometown(account_, hometown);
                    if (result) {
                        MessageUtil.success("籍贯修改成功");
                        account_.setHometown(hometown);
                    } else MessageUtil.error("[内部错误]籍贯修改失败");
                }
                case 5 -> {
                    MessageUtil.info("请输入新电话号码：");
                    String phone = IOUtil.parseInput();
                    result = UserDao.updatePhone(account_, phone);
                    if (result) {
                        MessageUtil.success("电话号码修改成功");
                        account_.setPhone(phone);
                    } else MessageUtil.error("[内部错误]电话号码修改失败");
                }
            }
        }
        Navigator.pop();
    }
}
